package org.bright.controller;

import org.bright.dto.OrderItemDTO;
import org.bright.entity.R;
import org.bright.service.IPurchaseOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * <p>
 * 采购订单表 记录采购订单信息 前端控制器
 * </p>
 *
 * @author bright
 * @since 2024-10-21
 */
@RestController
@RequestMapping("/purchaseOrders")
@CrossOrigin
public class PurchaseOrdersController {
    @Autowired
    private IPurchaseOrdersService purchaseOrdersService;
//   新增订单
    @PostMapping("/insertOrders")
    public R createPurchaseOrders(@RequestBody List<OrderItemDTO> orderItems) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
        return R.success(purchaseOrdersService.createOrders(orderItems));
    }
//查询所有订单
    @GetMapping("/selectAll")
    public R selectAll(@RequestParam Integer pageNum) {
        return R.success(purchaseOrdersService.selectAll(pageNum));
    }


//    查询供应商名称
    @GetMapping("/querySupplierName")
    public R querySupplierName(@RequestParam String orderId) {
        return R.success(purchaseOrdersService.selectAllByOrderId(orderId));
    }
    //子订单发货按钮功能
    @GetMapping("/updateStatus")
    public R updateStatus(@RequestParam("orderId") String orderId,
                          @RequestParam("supplierId") int supplierId) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
        return R.success(purchaseOrdersService.updateStatus(orderId, supplierId));
    }
//大订单收货按钮
    @PostMapping("/updateStatusByOrderId")
    public R updateStatusByOrderId(@RequestParam("orderId") String orderId) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
        return R.success(purchaseOrdersService.updateStatusByOrderId(orderId));
    }
}
